package eu.fishcode.android.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.Timer;

import eu.fishcode.android.utils.Utils;


public class SplashScreen extends JWindow implements ActionListener {

	private int currentFrame = 0;
	private final ImageIcon[] splashImages = new ImageIcon[19];
	private final JLabel splashLabel;
	private final Timer timer = new Timer(60, this);
	private final JLabel titleLabel;

	public SplashScreen() {
		final DecimalFormat nf = new DecimalFormat("00000");
		for (int i = 0; i < this.splashImages.length; i++) {
			final String nI = nf.format(i);
			this.splashImages[i] = Utils.getIcon("splash/android_" + nI
					+ ".png");
		}
		this.splashLabel = new JLabel(this.splashImages[0]);
		this.titleLabel = new JLabel("Android Boot Animation Editor");
		this.titleLabel.setForeground(Color.WHITE);
		this.titleLabel.setFont(new Font("Verdana", Font.BOLD, 16));
		this.titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(this.titleLabel, BorderLayout.PAGE_START);
		this.getContentPane().add(this.splashLabel, BorderLayout.CENTER);
		this.getContentPane().setBackground(Color.BLACK);
		this.pack();
		this.setSize(300, 200);
		this.setBackground(Color.BLACK);
		this.setLocationRelativeTo(null);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.currentFrame++;
		if (this.currentFrame > 18) {
			this.currentFrame = 0;
		}
		this.splashLabel.setIcon(this.splashImages[this.currentFrame]);
		this.repaint();
	}

	@Override
	public void setVisible(boolean b) {
		super.setVisible(b);
		if (b) {
			this.timer.start();
		} else {
			this.timer.stop();
		}
	}

}
